<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="com.sinodata.bsm.center.purview.FunctionConstants"%>
<%@ page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%boolean editAuthFlag = UserFunctionValidate.resFunctionValidate(Long.valueOf(String.valueOf(request.getAttribute("resId"))), FunctionConstants.EDIT_RES);%>
<html>
  <head>
    <title>自定义指标</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>  
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/table.jsp"%>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#propertys").dataTable({
				"bProcessing": true, 
				"bServerSide": true,
				"sServerMethod": 'POST',
				"sAjaxSource":"property!list.action?resId=${resId}",
				"bPaginate":false,
				"bSort": true,
				"bFilter":false,
				"sPaginationType": 'full_numbers',
				"aoColumns":[
					<% if(editAuthFlag){%>
						{"mDataProp":"operate","bSortable": false,"fnCreatedCell":function(nTd, sData, oData, iRow, iCol){
							$(nTd).html("<div style='width:50px; padding-left:22px;display:${hasEditResPrivilege?'block':'none'}'><a href='#"+oData.id+"' onclick='openUpdateWindow("+oData.id+")'><img src='../style/default/bsm/images/edit.gif' title='修改'></a>&nbsp;&nbsp;<a href='#' onclick='del("+oData.id+")'><img src='../style/default/bsm/images/delete.gif' title='删除' /></a> </div>");
						}},
					<%}%>	
					{"mDataProp":"name","bSortable": false},
					{"mDataProp":"dataTypeName","bSortable": false},
					{"mDataProp":"desc","bSortable": false},
					{"mDataProp":"id","bVisible":false}
				],
				"oLanguage":{
					"sZeroRecords": "没有找到记录",
					"sInfo":"",
					"sInfoEmpty": ""
				}
			});
		});
		
		function openInputWindow(){
			var url ='iframe:resource/property!input.action?resId=${resId}';
		    var opt =  {title:'新增指标',id:'inputPropertyWindow',width:555,height:270,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
		    getTopWin(window).openjBox(url,opt,'inputPropertyWindow',window);
		}
		
		addProperty = function (entity){
			$.ajax({
					type:'post',
					url:'property!save.action?resId=${resId}',
					data:entity,
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						var oTable =$('#propertys').dataTable();
						getTopWin(window).getMessageBox().tip('添加指标成功。', 'success'); 
						oTable.fnClearTable(); 
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
					}
			});
		}
		
		function del(id){
			if(isDel(id)==true){
				getTopWin(window).getMessageBox().alert('指标已被删除',"");
				return;
			}
			 var submit = function(v, h, f){
				  	if (v == 'ok'){
				  		getTopWin(window).getMessageBox().tip("正在删除资源指标...", 'loading');
						$.ajax({
								type:'post',
								url:'property!delete.action',
								data:{id:id},
								global:false,
								async:false,
								cache:false,
								dataType:'json',
								success:function(data,textStatus){
								    	var oTable =$('#propertys').dataTable();
										window.setTimeout(function () {
										    if(data.flag=="false"){
								    	         getTopWin(window).getMessageBox().tip(data.info,"info");
								   		    }else{
												getTopWin(window).getMessageBox().tip('删除指标成功。', 'success'); 
											    oTable.fnClearTable(); 
											}
										},1500);
								    
									
								},
								error:function(){
									getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
								}
						});
				}
			};
			getTopWin(window).getMessageBox().confirm('确定删除?','提示',submit);
		}
		
		
		function openUpdateWindow(id){
			if(isDel(id)==true){
				getTopWin(window).getMessageBox().alert('指标已被删除',"");
				return;
			}
			var url ='iframe:resource/property!initUpdate.action?resId=${resId}&id='+id;
		    var opt =  {title:'修改指标',id:'updateProperty',width:555,height:270,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
		    getTopWin(window).openjBox(url,opt,'updatePropertyWindow',window);
		}
		
		update = function(property){
			if(isDel(property.id)){
				getTopWin(window).getMessageBox().tip('指标已被删除',"");
				var table =$("#propertys").dataTable();
			 	table.fnClearTable();
			 	return;
			}
		
			$.ajax({
					type:'post',
					url:'property!save.action?resId=${resId}',
					data:property,
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						var oTable =$('#propertys').dataTable();
						getTopWin(window).getMessageBox().tip('修改指标成功。', 'success'); 
						oTable.fnClearTable(); 
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
					}
			});
		}
		
		//验证是否删除
		function isDel(id){
			var isFlag = false;
			$.ajax({
					type:'post',
					url:'property!isDelete.action',
					data:{id:id},
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						isFlag = data;
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('检验指标是否删除出错!','错误');
					}
			});
			return isFlag;
		}
	</script>

  </head>
  
  <body>
    	<div class ="toolbar" id="toolbar" style="display:${hasEditResPrivilege?'block':'none'}">
			<a href="#" onclick="openInputWindow()" class="page_add" >新增指标</a>
		</div>
		<table width="100%"  id="propertys" cellpadding="0" cellspacing="0" border="0" class="display"  >
			<thead>
    	 		<tr>
    	 			<% if(editAuthFlag){%>
    	 			<th width="90px">操作</th>
    	 			<%}%>	
    	 			<th width="120px">指标名称</th>
    	 			<th width="120px">数据类型及单位</th>
    	 			<th>描述</th>
    	 		</tr>
    	 	</thead>
		</table>
  </body>
</html>
